package cn.yunhe.controller;

import cn.yunhe.pojo.Member;
import cn.yunhe.pojo.MemberCustom;
import cn.yunhe.pojo.ResultRequest;
import cn.yunhe.pojo.ResultResponse;
import cn.yunhe.service.IMemberService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.RolesAllowed;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    IMemberService memberService;

    @RolesAllowed("ADMIN")
    @RequestMapping("/queryAll")
    public ResultResponse queryAll(@RequestBody ResultRequest<Member> resultRequest){
        ResultResponse resultResponse = new ResultResponse();
        List<Member> memberList = memberService.queryAll(resultRequest);
        //通过PageHelper获取总数据条数
        PageInfo pageInfo = new PageInfo(memberList);
        resultResponse.setData(memberList);
        resultResponse.setCount(pageInfo.getTotal());
        return resultResponse;
    }


    @RequestMapping("/query")
    public Map<String,Object> query(Integer page ,Integer limit){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("page", page);
        map.put("limit", limit);
        return map;
    }

    @RequestMapping("/insert")
    public Map<String,Object> insert(Member member){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("member", member);
        return map;
    }


    @RequestMapping("/insertMap")
    public Map<String,Object> insertMap(@RequestBody Map<String,Object> requestMap){
        return requestMap;
    }

    @RequestMapping("/insertCustom")
    public Map<String,Object> insertCustom(@RequestBody MemberCustom memberCustom){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("memberCustom", memberCustom);
        return map;
    }

}
